/*
 * HttpResponse.h
 *
 *  Created on: 01/06/2014
 *      Author: vitor
 */

#ifndef HTTPRESPONSE_H_
#define HTTPRESPONSE_H_

#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "TCPSocket.h"

class HttpResponse {
private:
	TCPSocket socket;
	std::map<std::string, std::string> headers;
	std::vector<char> data;
	int data_size = 0;

public:
	enum ResponseStatus {
		OK = 200, NOT_FOUND = 404, SEE_OTHER = 303
	};

	HttpResponse(TCPSocket socket, ResponseStatus status);
	void add_header(const char* name, const char* value);
	void set_data(std::vector<char> data);
	void set_status(ResponseStatus status) {
		this->status = status;
	}
	void send_response();
	virtual ~HttpResponse();

private:
	ResponseStatus status;

	void write_bytes(std::vector<char>& response, const char* data_to_write,
			int data_size);
	void write_status_line(std::vector<char>& response);
	void write_headers(std::vector<char>& response);
	void write_data(std::vector<char>& response);
	void get_status_string(char* result);
};

#endif /* HTTPRESPONSE_H_ */
